#include"../include/data_process.h"

int h_min2 = 4;
int h_max2 = 75;
int s_min2 = 55;
int s_max2 = 255;
int v_min2 = 45;
int v_max2 = 255;

vector<vector<Point> > contours;
RotatedRect end_rect;
Point2f rect[4];
Rect show_rect;

Mat color_infer2(Mat src,Mat dst)
{
    cvtColor(src, dst, COLOR_BGR2HSV);//转为HSV
    inRange(dst, Scalar(h_min2, s_min2, v_min2), Scalar(h_max2, s_max2, v_max2), dst);

    Mat element2 = getStructuringElement(MORPH_RECT, Size(5, 5));  
    Mat element1 = getStructuringElement(MORPH_RECT, Size(7, 7));  

    erode(dst, dst, element2);//腐蚀 
    dilate(dst, dst, element1);//膨胀
    return  dst;
}   


void process2(Mat src,Mat dst,string name)
{   
    dst = color_infer2(src,dst);
    findContours(dst,contours,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
    
    for(auto i : contours)
    {
    if(i.size()>30)
    {	
    end_rect = minAreaRect(i);
    end_rect.points(rect);
    //cout<<"中心点坐标： "<<end_rect.center<<endl;
    circle(src, Point(end_rect.center.x, end_rect.center.y), 5, Scalar(0, 255, 0), -1, 8);  //绘制最小外接矩形的中心点
    for(int j=0; j<4; j++)
    {
        line(src, rect[j], rect[(j+1)%4], Scalar(0, 0, 255), 2, 8);  //绘制最小外接矩形每条边
    }
}
    imshow(name,src);
    waitKey(10);
    }
}
